package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private UserDao userDao;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.findByQueryString(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();
        return new PageResult(total, rows);
    }

    @Override
    public Integer[] findPermissionids(Integer id) {
        return roleDao.findPermissionids(id);
    }

    @Override
    public Role findByid(Integer id) {
        return roleDao.findByid(id);
    }

    @Override
    public void add(Role role, Integer[] menuids, Integer[] permissionids) {
        roleDao.add(role);
        if (menuids.length>0) {
            for (Integer menuid : menuids) {
                int id = menuDao.findparentid(menuid);
                if (id > 0) {
                    int count = roleDao.findByParent(id);
                    if (count<0){
                        roleDao.addparentid(role.getId(), id);
                    }
                }
            }
            roleDao.addmenuids(role.getId(), menuids);
        }
        if (permissionids.length>0) {
            roleDao.addpermissionids(role.getId(), permissionids);
        }
    }

    @Override
    public Integer[] findMenuids(Integer id) {
        return roleDao.findMenuids(id);
    }

    @Override
    public void deleteByid(Integer id) {
        int count = userDao.findroleid(id);
        if (count>0) {
            userDao.deleteroleid(id);
        }
        Integer[] menuids = roleDao.findMenuids(id);
        if (menuids.length>0) {
            roleDao.deleteMenuids(id);
        }
        Integer[] permissionids = roleDao.findPermissionids(id);
        if (permissionids.length>0) {
            roleDao.deletePermissionids(id);
        }
        roleDao.deleteByid(id);
    }

    @Override
    public void update(Role role, Integer[] menuids, Integer[] permissionids) {
        roleDao.update(role);
        if (menuids.length>0) {
            roleDao.deleteMenuids(role.getId());
            for (Integer menuid : menuids) {
                int id = menuDao.findparentid(menuid);
                if (id > 0) {
                    int count = roleDao.findByParent(id);
                    if (count<0){
                        roleDao.addparentid(role.getId(), id);
                    }
                }
            }
            roleDao.addmenuids(role.getId(), menuids);
        }
        if (permissionids.length>0) {
            roleDao.deletePermissionids(role.getId());
            roleDao.addpermissionids(role.getId(), permissionids);
        }

    }
}
